//
 

#import "NT_CategoryEditViewController.h"
#import "NT_EditCategoryTableViewCell.h"

@interface NT_CategoryEditViewController ()<UITableViewDelegate, UITableViewDataSource, NT_EditCategoryTableViewDelegate>
/** */
@property (nonatomic, strong) UITableView *editTableView;
/** */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** */
@property (nonatomic, strong) NT_CoreDataBase *data;


@end

@implementation NT_CategoryEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setnavBatTitle:@"Edit"];
    _data = [[NT_CoreDataBase alloc]initWithCoreData:@"NoteCategory" modelName:@"NoteData" success:^{} fail:^(NSError * _Nullable error) { }];
    
    [self.view addSubview:self.editTableView];
    [self getCategoryDatassssss];
}

- (void)actionBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//#pragma mark ----------------- tableViewDelegate ----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NT_EditCategoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[NT_EditCategoryTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
   cell.model = _dataArray[indexPath.row];
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kRelativeHeight(10);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
//MARK:
- (void)sendCellData:(NT_EditCategoryTableViewCell *)cell {
        //
    __weak typeof(self) weakSelf = self;
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:@"Are you sure you want to delete it?" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * sureAction = [UIAlertAction  actionWithTitle:@"sure"style:UIAlertActionStyleDefault handler:^(UIAlertAction*_Nonnullaction){
        //
        [weakSelf.data ntDeleteEntity:cell.model key:[NSString stringWithFormat:@"id=%@",cell.model.id] success:^{
            [weakSelf getCategoryDatassssss];
        } fail:^(NSError * _Nullable error) {
        }];
        
    }];
    UIAlertAction * cancelAction = [UIAlertAction  actionWithTitle:@"cancel"style:UIAlertActionStyleCancel handler:^(UIAlertAction*_Nonnullaction){
        
    }];
    [alertC addAction:sureAction];
    [alertC addAction:cancelAction];
    [self presentViewController:alertC animated:YES completion:nil];
    
}


#pragma mark ----------------- 懒加载 ----------------
- (UITableView *)editTableView {
    if (!_editTableView) {
        _editTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UI_NAV_BAR_HEIGHT, kMainScreenWidth, kMainScreenHeight) style:UITableViewStylePlain];
        _editTableView.dataSource = self;
        _editTableView.delegate   = self;
        _editTableView.tableFooterView = [UIView new];
        _editTableView.rowHeight  = kRelativeHeight(50);
    }
    return _editTableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}

//MARK:
- (void)getCategoryDatassssss {
    //
    [_data ntReadEntity:nil ascending:NO filterStr:nil success:^(NSArray * _Nonnull results) {
        if (!results) return ;
        //
        self.dataArray = [results copy];
        [self.editTableView reloadData];
        
    } fail:^(NSError * _Nullable error) {
        
    }];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
